package com.qf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.mapper.DtsUserAccountMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsUser;
import com.qf.pojo.DtsUserAccount;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private DtsUserMapper dtsUserMapper;

    @Autowired
    private DtsUserAccountMapper dtsUserAccountMapper;

    /**
     * 查询会员列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     *
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"total": 197,
     * 		"items": [
     * 			{
     * 				"id": 21,
     * 				"username": "oVpFH4x2Xy梅***",
     * 				"password": "oVpFH4x2Xy******",
     * 				"gender": 2,
     * 				"lastLoginTime": "2019-12-20 13:30:34",
     * 				"lastLoginIp": "223.104.21.236",
     * 				"userLevel": 0,
     * 				"nickname": "梅***",
     * 				"mobile": "******",
     * 				"avatar": "https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83erUKpiakLVxyxloQx9bJUfmNr8DxrNPxpnD7p2tzIcNSFAqZ6icnov4R5TwzITX0TibN3F6D8fVhdnpQ/132",
     * 				"weixinOpenid": "oVpFH4x2Xy******",
     * 				"status": 0,
     * 				"addTime": "2119-12-30 22:53:39",
     * 				"updateTime": "2119-12-30 22:53:39",
     * 				"deleted": false,
     * 				"shareUserId": 1
     * 			},
     * 			{
     * 				"id": 115,
     * 				"username": "oVpFH4_5YD双***",
     * 				"password": "oVpFH4_5YD******",
     * 				"gender": 2,
     * 				"lastLoginTime": "2019-12-05 21:56:32",
     * 				"lastLoginIp": "113.77.83.163",
     * 				"userLevel": 0,
     * 				"nickname": "双***",
     * 				"mobile": "******",
     * 				"avatar": "https://wx.qlogo.cn/mmopen/vi_32/61BNy5C06ey8dO1hr5XbfeIVgGsaibgrWpAHxiariaxDhYt3ydE2Sp0DD53wKAWYJQt8MX3jG1f7PudW5xLQnp9icQ/132",
     * 				"weixinOpenid": "oVpFH4_5YD******",
     * 				"status": 0,
     * 				"addTime": "2119-12-30 22:22:34",
     * 				"updateTime": "2119-12-30 22:22:34",
     * 				"deleted": false,
     * 				"shareUserId": 2
     * 			}
     * 		]
     * 	},
     * 	"errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:user:list")
    @RequiresPermissionsDesc(menu = "用户管理",button = "会员管理") //菜单 按钮
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String username,String mobile,
                       String sort,String order){

        //分页
        Page<DtsUser> pageHelp = new Page<>(page,limit);

        //写入条件
        QueryWrapper<DtsUser> wrapper = new QueryWrapper<DtsUser>();
        if (!StringUtils.isEmpty(username)){
            wrapper.like("username", username);
        }
        if (!StringUtils.isEmpty(mobile)){
            wrapper.like("mobile", mobile);
        }
        wrapper.eq("deleted", false);
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),sort);
        wrapper.orderByDesc("desc".equalsIgnoreCase(order),sort);


        /**
         * <E extends IPage<T>>
         * 调用分页查询
         */
        IPage<DtsUser> iPage = dtsUserMapper.selectPage(pageHelp, wrapper);
        //查询总数
        Integer count = dtsUserMapper.selectCount(wrapper);

        List<DtsUser> dtsUsers = iPage.getRecords();

        DtsUser [] items = new DtsUser[dtsUsers.size()];

        for (int i = 0; i < dtsUsers.size(); i++) {
            items[i] = dtsUsers.get(i);
        }

        //封装入数据
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", count);
        data.put("items",items);

        return ResponseUtil.ok(data);
    }


    //会员审批、
    @RequestMapping("/approveAgency")
    @RequiresPermissions("admin:user:approveAgency")
    @RequiresPermissionsDesc(menu = "会员管理",button = "会员审批")
    public Object approveAgency(){
        return ResponseUtil.ok();
    }

    /**
     * 会员推广代理
     * @param id
     * @return
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"id": 5,
     * 		"userId": 71,
     * 		"remainAmount": 0,
     * 		"totalAmount": 0,
     * 		"createTime": "2019-10-28 11:21:40",
     * 		"modifyTime": "2019-12-15 12:32:14",
     * 		"settlementRate": 8,
     * 		"status": 1
     * 	},
     * 	"errmsg": "成功"
     * }
     */
    @GetMapping("/detailApprove")
    @RequiresPermissions("admin:user:detailApprove")
    @RequiresPermissionsDesc(menu = "会员管理",button = "会员推广代理")
    public Object detailApprove(Integer id){

        QueryWrapper<DtsUserAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);

        DtsUserAccount data = dtsUserAccountMapper.selectOne(wrapper);
        return ResponseUtil.ok(data);
    }














}
